package com.alexgaoyh.MutiModule.Dubbo.RWSeperate.consumer.web.demo;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.MutiModule.common.myPage.MyPageView;
import com.MutiModule.common.twitter.IDGenerator.instance.IdWorkerInstance;
import com.MutiModule.common.utils.StringUtilss;
import com.MutiModule.common.vo.mybatis.pagination.Page;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.api.mutiDatabase.read.IMutiDatabaseReadService;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.api.mutiDatabase.write.IMutiDatabaseWriteService;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.consumer.web.base.BaseController;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.persist.mutiDatabase.MutiDatabase;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.persist.mutiDatabase.MutiDatabaseExample;
import com.alibaba.dubbo.config.annotation.Reference;

/**
*
* dubbo 消费者
*
* @Reference 注解需要在 dubbo consumer中做如下配置
*
* <dubbo:annotation/>
*	<context:component-scan base-package="net.aimeizi.dubbo.controller">
*	<context:include-filter type="annotation" expression="com.alibaba.dubbo.config.annotation.Reference"/>
* </context:component-scan>
*
* 若要使用@Autowired或@Resource注解需要显式声明bean
*
* 使用@Autowired或@Resource注解时需要使用dubbo:reference来声明
* <dubbo:reference interface="net.aimeizi.dubbo.service.UserService" id="userService"/>
* <dubbo:reference interface="net.aimeizi.dubbo.service.DemoService" id="demoService"/>
*
* 以上的配置均需要在spring mvc的DispatcherServlet配置中显式配置dubbo consumer的配置.如/WEB-INF/applicationContext-dubbo-consumer.xml 否则在Controller中服务报NullPointException
* <servlet>
*	<servlet-name>mvc-dispatcher</servlet-name>
*		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
*	<init-param>
*	<param-name>contextConfigLocation</param-name>
*		<param-value>/WEB-INF/applicationContext*.xml,/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
*	</init-param>
*	<load-on-startup>1</load-on-startup>
* </servlet>
*
*/
@Controller
@RequestMapping(value="manager/mutiDatabase")
public class MutiDatabaseController extends BaseController<MutiDatabase>{

	@Reference(group="readService", interfaceName="com.alexgaoyh.MutiModule.Dubbo.RWSeperate.api.mutiDatabase.read.IMutiDatabaseReadService", init=true)
	private IMutiDatabaseReadService readService;
	
	@Reference(group="writeService", interfaceName="com.alexgaoyh.MutiModule.Dubbo.RWSeperate.api.mutiDatabase.write.IMutiDatabaseWriteService", init=true)
	private IMutiDatabaseWriteService writeService;

	@Override
	public ModelAndView list(ModelAndView model, HttpServletRequest request) {
		
		super.list(model, request);
		
		MyPageView<MutiDatabase> pageView = decorateCondition(request);

		model.addObject("pageView", pageView);
		
		model.setViewName("demo/mutiDatabase/list");
		
		return model;
		
	}
	
	/**
	 * 封装参数部分
	 * @param request
	 * @return
	 */
	protected MyPageView<MutiDatabase> decorateCondition(HttpServletRequest request) {
		
		String currentPageStr = request.getParameter("currentPage");
		if(StringUtilss.isEmpty(currentPageStr)) {
			currentPageStr = "1";
		}
		
		String recordPerPageStr = request.getParameter("recordPerPage");
		if(StringUtilss.isEmpty(recordPerPageStr)) {
			recordPerPageStr = "10";
		}

		int beginInt = Integer.parseInt(currentPageStr) >=1 ? Integer.parseInt(currentPageStr) : 1;
		
		Page page = new Page((beginInt - 1)*Integer.parseInt(recordPerPageStr), Integer.parseInt(recordPerPageStr));
		
		
		MutiDatabaseExample exampleWithOutPage = new MutiDatabaseExample();
		exampleWithOutPage.setOrderByClause("id desc");
		com.alexgaoyh.MutiModule.Dubbo.RWSeperate.persist.mutiDatabase.MutiDatabaseExample.Criteria criteria = 
				 exampleWithOutPage.createCriteria();
		
		String listSearchNameStr = request.getParameter("listSearchName");
		if(StringUtilss.isNotEmpty(listSearchNameStr)) {
			criteria.andNameLike("%" + listSearchNameStr + "%");
		}
		
		MyPageView<MutiDatabase> pageView = readService.generateMyPageViewVO(exampleWithOutPage, page);
		
		return pageView;
	}
	
	@Override
	public ModelAndView add(ModelAndView model, HttpServletRequest request) {
		
		super.add(model, request);
		model.setViewName("demo/mutiDatabase/add");
		return model;
		
	}
	
	/**
	 * 保存
	 * 
	 * @return
	 * @throws IOException
	 */
	@Override
	public ModelAndView doSave(ModelAndView model, HttpServletRequest request, MutiDatabase entity) throws IOException  {
		super.doSave(model, request, entity);
		
		try {
			beforeDoSave(request, entity);
			writeService.insert(entity);
			afterDoSave(request, entity);
		} catch (Exception e) {
			
		} finally {
			model.setViewName("redirect:list");
		}

		return model;
	}

	/**
	 * 调用保存方法之前进行的方法调用
	 * @param request
	 * @param entity 对应实体信息
	 * @throws Exception
	 */
	protected void beforeDoSave(HttpServletRequest request, MutiDatabase entity) throws Exception {
		entity.setId(IdWorkerInstance.getId());
	}
	
	/**
	 * 电泳保存方法之后进行的方法调用
	 * @param request
	 * @param entity 对应实体信息
	 * @throws Exception
	 */
	protected void afterDoSave(HttpServletRequest request, MutiDatabase entity) throws Exception {
		
	}
	
	@Override
	public ModelAndView doUpdate(ModelAndView model, HttpServletRequest request, MutiDatabase entity) throws IOException {
		super.doUpdate(model, request, entity);
		
		try {
			beforeDoUpdate(request, entity);
			writeService.updateByPrimaryKey(entity);
			afterDoUpdate(request, entity);
		} catch (Exception e) {
			
		} finally {
			model.setViewName("forward:list");
		}

		return model;
	}
	
	/**
	 * 调用更新操作之前进行的操作
	 * @param request
	 * @param entity
	 * @throws Exception
	 */
	protected void beforeDoUpdate(HttpServletRequest request, MutiDatabase entity) throws Exception {
		
	}
	
	/**
	 * 调用更新操作之后进行的操作
	 * @param request
	 * @param entity
	 * @throws Exception
	 */
	protected void afterDoUpdate(HttpServletRequest request, MutiDatabase entity) throws Exception {
		
	}
	
	@Override
	public ModelAndView show(@PathVariable("id") Long id, ModelAndView model, HttpServletRequest request) throws IOException  {
		
		super.show(id, model, request);
		
		MutiDatabase entity = readService.selectByPrimaryKey(id);
		model.addObject("entity", entity);
		model.setViewName("demo/mutiDatabase/show");
		return model;
	}
	
	@Override
	public ModelAndView edit(@PathVariable("id") Long id, ModelAndView model, HttpServletRequest request) throws IOException  {
		
		super.edit(id, model, request);
		
		MutiDatabase entity = readService.selectByPrimaryKey(id);
		model.addObject("entity", entity);
		model.setViewName("demo/mutiDatabase/edit");
		return model;
	}
}
